{-# LANGUAGE TemplateHaskell #-}

module MyTools.MyMacros where
import Language.Haskell.TH
import Language.Haskell.TH.Syntax
import System.Console.ANSI

------------------------------------- ANSI 颜色代码定义
reset :: String
reset = "\x1b[0m"

red :: String
red = "\x1b[31m"

green :: String
green = "\x1b[32m"

yellow :: String
yellow = "\x1b[33m"

bold_blue :: String
bold_blue = "\x1b[1;34m"

light_blue :: String
light_blue = "\x1b[2;34m"

bold_purple :: String
bold_purple = "\x1b[1;35m"

bold_cyan :: String
bold_cyan = "\x1b[1;36m"

bold_white :: String
bold_white = "\x1b[1;37m"

black :: String
black = "\x1b[30m"

blue :: String
blue = "\x1b[34m"

purple :: String
purple = "\x1b[35m"

cyan :: String
cyan = "\x1b[36m"

white :: String
white = "\x1b[37m"

-- 带颜色的打印函数
myPrt :: String -> String -> IO ()
myPrt color msg = do
    putStr $ color ++ msg ++ reset

myPrtLn :: String -> String -> IO ()
myPrtLn color msg = do
    putStrLn $ color ++ msg ++ reset

-- -- 带颜色的打印函数，带颜色选择
-- myPrt' :: String -> String -> String -> IO ()
-- myPrt' fileClr lineClr msg = do
--     loc <- location
--     let fileName = loc_filename loc
--         lineNo = fst (loc_start loc)
--     putStrLn $ sep ++ fileClr ++ fileName ++ reset ++ ":" ++ lineClr ++ show lineNo ++ reset ++ bold_blue ++ msg ++ reset




-- 分割线定义
sep :: String
sep = "-------------------------"

-------------------------- 带颜色的位置打印
_myLoc :: Q Exp
_myLoc = do
    loc <- location
    let fileColor = green
        -- lineColor = yellow
        lineColor = green
        fileName = loc_filename loc
        lineNo = fst (loc_start loc)
    [| do
        --  setSGR [Reset]
         setSGR [SetColor Foreground Vivid Cyan]
         putStrLn $( liftE $
           sep ++
           -- fileColor ++
           fileName ++ ":" ++
           -- lineColor ++
           show lineNo )
        --  setSGR [Reset]
         setSGR [SetColor Foreground Vivid Blue]
     |]
  where
    liftE = lift

-------------------------- 带动态颜色选择的高级版本
_myLoc' :: String -> String -> Q Exp
_myLoc' fileClr lineClr = do
    loc <- location
    let fileName = loc_filename loc
        lineNo = fst (loc_start loc)
    [| putStrLn $( liftE $
         sep ++
         fileClr ++ fileName ++ reset ++ ":" ++
         lineClr ++ show lineNo ++ reset)
     |]
  where
    liftE = lift

-------------------------- 生成打印文件名和行号的表达式
_myLocA :: Q Exp
_myLocA = do
    loc <- location   -- 获取调用位置信息
    let fileName = loc_filename loc
        _myLine = fst (loc_start loc)  -- 获取起始行号
    [| putStrLn $(liftE $ sep ++ fileName ++ ":" ++ show _myLine) |]
  where
    liftE = lift  -- 将字符串提升为表达式级别

-------------------------- 单独获取文件名
_myFile :: Q Exp
_myFile = do
    loc <- location
    lift (loc_filename loc)

-------------------------- 单独获取行号
_myLine :: Q Exp
_myLine = do
    loc <- location
    lift (fst (loc_start loc))